package com.weilai.common.controller;

import com.weilai.common.model.M;
import com.weilai.common.model.MedalRecord;
import com.weilai.common.model.SysUser;
import com.weilai.common.service.MedalRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Song on 2017/6/6.
 */
@Controller
@RequestMapping("/medalRecord")
public class MedalRecordController {
    @Autowired
    MedalRecordService medalRecordService;

    /**
     * 查找用户的奖章
     * @param medalRecord
     * @param model
     * @return
     */
    @RequestMapping("/findByUser")
    public String findByUser(@ModelAttribute MedalRecord medalRecord, Model model){
        List<MedalRecord> record = medalRecordService.findByUser(medalRecord);
        model.addAttribute("record",record);
        return "";
    }


    /**
     * 前台用户查找奖章数量
     * @return
     */
    @RequestMapping(value = "findByUserOfMedalRecordCount",method = RequestMethod.GET)
    @ResponseBody
    public Integer findByUserOfMedalRecordCount(HttpServletRequest request){
        SysUser user = (SysUser) request.getSession().getAttribute("user");
        MedalRecord medalRecord = new MedalRecord();
        medalRecord.setUser(user);
        MedalRecord record = medalRecordService.findByUserOfMedalRecordCount(medalRecord);
        return record.getMedalCount();
    }


    /**
     * 后台查询用户奖章数量
     * @param ids
     * @return
     */
    @RequestMapping(value = "houtaifindByUserOfMedalRecordCount",method = RequestMethod.GET)
    @ResponseBody
    public Integer findByUserOfMedalRecordCount(@RequestParam String ids){
//        SysUser user = (SysUser) request.getSession().getAttribute("user");

        SysUser user = new SysUser();
        user.setId(Integer.parseInt(ids));
        MedalRecord medalRecord = new MedalRecord();
        medalRecord.setUser(user);
        MedalRecord record = medalRecordService.findByUserOfMedalRecordCount(medalRecord);
        return record.getMedalCount();
    }



    @RequestMapping(value = "saveByUserOfMedalCount",method = {RequestMethod.POST})
    @ResponseBody
    public Integer saveByUserOfMedalCount(@RequestBody M m){
        String medalRecordStr = m.getMedalCount();
        String id = m.getId();
        SysUser sysUser = new SysUser();
        sysUser.setId(Integer.parseInt(id));
        MedalRecord medalRecord = new MedalRecord();
        medalRecord.setUser(sysUser);
        medalRecord.setMedalCount(Integer.parseInt(medalRecordStr));
        medalRecordService.saveByUserOfMedalCount(medalRecord);
        return medalRecord.getMedalCount();
    }


    /**用户查询奖章记录
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "findMedalRecord",method = RequestMethod.POST)
    @ResponseBody
    public List<MedalRecord> findMedalRecord(HttpServletRequest request){
        //获得当前用户的
        SysUser user = (SysUser) request.getSession().getAttribute("user");
//        SysUser user = new SysUser();
//        user.setId(2);
        MedalRecord medalRecord = new MedalRecord();

        medalRecord.setUser(user);
        List<MedalRecord> medalRecordList = medalRecordService.findByUser(medalRecord);
        return medalRecordList;
    }

}


